package com.example.mqttdemo.controller;


import com.example.mqttdemo.mqtt.MqttPushClient;
import com.example.mqttdemo.utils.MqttDataUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author:Dong
 * @Date：2020/8/26 16:12
 */
@Slf4j
@RestController
@RequestMapping("mqttDemo")
public class MqttDemoController {
    @Autowired
    private MqttPushClient mqttPushClient;

    /**
     * 通过MqttPushClient类publish方法的发送"这是一条测试消息"到名为"test_queue"的主题，如果需要拿到这条消息，
     * 需要在MqttSubClient类的subScribeDataPublishTopic方法进行配置和订阅该主题（这个DEMO已经配置好,并在连接mqtt服务器时就已经订阅），
     * 配置完成后 PushCallBack类的messageArrived方法会接收到已订阅主题接收到的消息(订阅主题后可以在该方法中处理接收到的消息)
     * <p>
     * Send "This is a test message" to the topic named "test_queue" through the publish method of the MqttPushClient class.
     * If you need to get this message,Need to configure and subscribe to the topic in the subScribeDataPublishTopic
     * method of the MqttSubClient class (this DEMO has been configured and subscribed when connecting to the mqtt server),
     * After the configuration is completed, the messageArrived method of the PushCallBack class will receive the message
     * received by the subscribed topic (you can process the received message in this method after subscribing to the topic)
     */

    @RequestMapping("testPublishMessage1")
    public void testPublishMessage() {
        log.info("测试");
        mqttPushClient.publish("test_queue", "这是一条测试消息");
    }

    @RequestMapping("testPublishMessage2")
    public void testPublishMessage2(String message) {

        mqttPushClient.publish("test_queue", message);
    }



    // RGB灯
    @GetMapping("/light2on")
    public String light2on() {
        log.info("控制开关RGB：");
        mqttPushClient.publish("Pegasus002/act2", "ledopen");

        return "打开RGB灯";
    }

    @GetMapping("/light2off")
    public String light2off() {
        log.info("控制开关RGB：");
        mqttPushClient.publish("Pegasus002/act2", "ledclose");

        return "关闭RGB灯";
    }

    // 射灯
    @GetMapping("/light0on")
    public String light0on() {
        log.info("控制开关射灯：");
        mqttPushClient.publish("Pegasus000/act0", "ledopen");

        return "打开射灯";
    }

    @GetMapping("/light0off")
    public String light0off() {
        log.info("控制开关射灯：");
        mqttPushClient.publish("Pegasus000/act0", "ledclose");

        return "关闭射灯";
    }

    // 窗帘
    @GetMapping("/curtainon")
    public String curtainon() {
        log.info("控制开关窗帘：");
        mqttPushClient.publish("Pegasus000/act0", "curtainopen");

        return "打开窗帘";
    }

    @GetMapping("/curtainoff")
    public String curtainoff() {
        log.info("控制开关窗帘：");
        mqttPushClient.publish("Pegasus000/act0", "curtainclose");

        return "关闭窗帘";
    }

    // 风扇
    @GetMapping("/fanon")
    public String fanon() {
        log.info("控制开关风扇：");
        mqttPushClient.publish("Pegasus001/act1", "fanopen");

        return "打开风扇";
    }

    @GetMapping("/fanoff")
    public String fanoff() {
        log.info("控制开关风扇：");
        mqttPushClient.publish("Pegasus001/act1", "fanclose");

        return "关闭风扇";
    }

    // 窗户
    @GetMapping("/left")
    public String windowon() {
        log.info("控制开关窗帘：");
        mqttPushClient.publish("Pegasus002/act2", "left");

        return "打开窗户";
    }

    @GetMapping("/right")
    public String windowoff() {
        log.info("控制开关窗帘：");
        mqttPushClient.publish("Pegasus002/act2", "right");

        return "关闭窗户";
    }

    // 台灯
    @GetMapping("/light1on")
    public String light1on() {
        log.info("控制开关主灯：");
        mqttPushClient.publish("Pegasus001/act1", "ledopen");

        return "打开台灯";
    }

    @GetMapping("/light1off")
    public String light1off() {
        log.info("控制开关主灯：");
        mqttPushClient.publish("Pegasus001/act1", "ledclose");

        return "关闭台灯";
    }

    //烟雾报警器
    @GetMapping("/smoke")
    public String getMqttData() {
        log.info("烟雾报警器数据查询：");
        return MqttDataUtils.getInstance().get("$oc/devices/Pegasus001/sys/properties/report/gas");

    }


}
